/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package espe.ccsoft.dao;

import espe.ccsoft.models.Accion;
import espe.ccsoft.orm.ORMBase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author misahaelfernandez
 */
public class AccionDao extends ORMBase<Accion> {

    public final void insert(final Accion accion) throws Exception {
        super.insertSql = "INSERT INTO ACCION ("
                + "PERM_ID,"
                + "ACC_NOMBRE"
                + ") VALUES(?, ?)";
        super.insert(new Object[]{
            accion.getId_permiso(),
            accion.getNombre(),});
    }

    public final Accion findAccesoVista(String nombre_controlador,
            String nombre_accion, int id_rol) throws Exception {

        super.findByPkSql = "SELECT ACC.ACC_ID,ACC.PERM_ID,ACC.ACC_NOMBRE FROM "
                + "ROL AS R, "
                + "ROL_ACCION AS RACC, "
                + "ACCION AS ACC, "
                + "PERMISO AS P WHERE "
                + "R.ROL_ID=RACC.ROL_ID "
                + "AND P.PERM_ID=ACC.PERM_ID "
                + "AND ACC.ACC_ID=RACC.ACC_ID "
                + "AND P.PERM_CONTROLADOR=? "
                + "AND ACC.ACC_NOMBRE=? "
                + "AND R.ROL_ID=?";

        return super.findByPK(new Object[]{
            nombre_controlador,
            nombre_accion,
            id_rol,});
    }

    public final List<Accion> findByNombre(String nombre) throws Exception {
        super.findSomeSql = "SELECT * FROM ACCION"
                + " WHERE (ACC_NOMBRE LIKE ?)";
        return super.findSome(new Object[]{
            nombre,});
    }

    public final String findNombreById(int id) throws Exception {
        super.findByPkSql = "SELECT * FROM ACCION"
                + " WHERE ACC_ID=?";
        return super.findByPK(new Object[]{
            id,}).getNombre();
    }

    public final List<Accion> findByPermisoId(int id_permiso) throws Exception {
        super.findSomeSql = "SELECT * FROM ACCION"
                + " WHERE PERM_ID=?";
        return super.findSome(new Object[]{
            id_permiso,});
    }

    public final List<Accion> findAllByPermisoRol(int id_rol, int id_permiso) throws Exception {
        super.findSomeSql = "SELECT ACC.ACC_ID, ACC.PERM_ID, ACC.ACC_NOMBRE "
                + "FROM ACCION AS ACC, ROL AS R, ROL_ACCION AS RACC, "
                + "PERMISO AS P, PERMISOS_ROL PROL "
                + "WHERE ACC.PERM_ID=P.PERM_ID "
                + "AND PROL.PERM_ID=P.PERM_ID "
                + "AND PROL.ROL_ID=R.ROL_ID "
                + "AND RACC.ROL_ID=R.ROL_ID "
                + "AND RACC.ACCPERM_ID=ACC.ACC_ID "
                + "AND R.ROL_ID=? AND P.PERM_ID=?";

        return super.findSome(new Object[]{
            id_rol,
            id_permiso,});
    }

    @Override
    public final List<Accion> findAll() throws SQLException, Exception {
        // Sentencia SQL para realizar la consulta a la BD:
        super.findAllSql = "SELECT * FROM ACCION";
        return super.findAll();
    }

    @Override
    protected Accion crearObjeto(final ResultSet rs) throws SQLException {
        Accion accion = new Accion();
        accion.setId(rs.getInt("ACC_ID"));
        accion.setId_permiso(rs.getInt("PERM_ID"));
        accion.setNombre(rs.getString("ACC_NOMBRE"));
        return accion;
    }
}
